import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_house_manager/api/user.dart';
import 'package:flutter_house_manager/controller/UserInfoController.dart';
import 'package:flutter_house_manager/utils/toast.dart';
import 'package:get/get.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({super.key});

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  UserInfocontroller controller = Get.find();
  TextEditingController nickController = TextEditingController();

  Widget getUserAvatar() {
    if (controller.userInfo["avatar"] != "") {
      return Image.network(
        controller.userInfo["avatar"],
        width: 30,
        height: 30,
      );
    }
    return Image.asset(
      'assets/images/avatar_1.jpg',
      width: 30,
      height: 30,
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    nickController.text = controller.userInfo["nickName"];
  }

  _saveUserInfo() async {
    if (nickController.text.isEmpty) {
      PromptAction.showWarning("用户昵称不能为空");
      return;
    }
    if (!RegExp(r'^[\u4e00-\u9fa5]{2,10}').hasMatch(nickController.text)) {
      PromptAction.showWarning("昵称必须是2-10个中文字符");
      return;
    }
    await updateUserInfoAPI({
      "nickName": nickController.text,
    });
    // 提示一下
    PromptAction.showSuccess("用户资料保存成功");
    controller.userInfo["nickName"] = nickController.text; // 更新对象里面的属性
    controller.updateUserInfo(controller.userInfo); // 通过Getx的更新方法驱动页面进行更新
  }

  showSelectDialog() {
    showBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 150,
            width: double.infinity,
            color: Colors.white,
            child: const Column(
              children: [
                SizedBox(
                  child: Row(
                    children: [Icon(Icons.camera_alt), Text('拍照')],
                  ),
                )
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('个人信息'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          SizedBox(
            height: 40,
            child: Row(
              children: [
                const Text(
                  '头像',
                  style: TextStyle(fontSize: 16),
                ),
                const Spacer(),
                GestureDetector(
                    onTap: () {
                      //弹出层
                      showSelectDialog();
                      PromptAction.showToast('更换头像');
                    },
                    child: Row(
                      children: [
                        ClipRRect(
                            borderRadius: BorderRadius.circular(50.0),
                            child: getUserAvatar()),
                        const Icon(Icons.arrow_forward_ios, size: 12)
                      ],
                    ))
              ],
            ),
          ),
          Row(
            children: [
              const Text('昵称'),
              const Spacer(),
              Expanded(
                child: TextField(
                  controller: nickController,
                  decoration: const InputDecoration(
                    hintText: '请输入昵称',
                    border: InputBorder.none,
                  ),
                  textAlign: TextAlign.right,
                ),
              ),
              const Icon(Icons.arrow_forward_ios, size: 12)
            ],
          ),
          const SizedBox(
            height: 20,
          ),
          Row(
            children: [
              Expanded(
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color.fromARGB(255, 85, 145, 175),
                    minimumSize: const Size(100, 50),
                  ),
                  onPressed: () {
                    _saveUserInfo();
                  },
                  child: const Text(
                    '保存',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                    ),
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
